<template>
  <div class="app-container">
    <a-title @clickPreview="preview" />
    <div class="input-wrap">
      <div class="input-label">当前管理员</div>
      <div class="input-content">
        <el-select
          v-model="adminList"
          multiple
          placeholder="请选择"
          @change="changevalue"
          :filterable="true"
        >
          <el-option
            v-for="item in options"
            :key="item.userId"
            :label="item.nickName"
            :value="item.userId"
          ></el-option>
        </el-select>
        <el-tag class="tipTag">
          <i class="el-icon-warning"/> 请直接下拉选择或取消管理员授权
        </el-tag>
      </div>
    </div>
    <div class="history">管理员变更历史记录</div>
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="姓名" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入姓名"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="getHistoryList"
        />
      </el-form-item>
      <el-form-item label="变更内容" prop="remark">
        <el-input
          v-model="queryParams.remark"
          placeholder="请输入变更内容"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="getHistoryList"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="getHistoryList">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
      stripe
      :data="tableData"
      v-loading="loading"
      @sort-change="tableSortChangeHistory"
      :default-sort="{prop: 'updateTime', order: 'descending'}"
    >
      <el-table-column
        label="时间"
        prop="updateTime"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime,'{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="姓名"
        prop="nickName"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      ></el-table-column>
      <el-table-column
        label="手机号码"
        prop="phonenumber"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      ></el-table-column>
      <el-table-column
        label="员工编号"
        prop="jobNo"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      ></el-table-column>
      <el-table-column
        label="变更内容"
        prop="remark"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      ></el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getHistoryList"
    />
    <!-- 预览 -->
    <el-dialog :visible.sync="openPreview" width="80%">
      <iframe :src="manualPreviewUrl" frameborder="0" width="100%" height="700px"></iframe>
    </el-dialog>
  </div>
</template>
<script>
import { adminAuthorizeList, adminAuthorizeHistoryList, cancelAdminAuthorize, addAdminAuthorize } from '@/api/projectleader/adminAuthorize'
import aTitle from '@/components/Title'
export default {
  name: 'adminAuthorize',
  components: { aTitle },
  data () {
    return {
      manualPreviewUrl: null,
      openPreview: false,
      loading: true,
      options: [],
      adminList: [],
      form: [],
      tableData: [],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickName: undefined,
        remark: undefined
      },
      userinfo: null
    }
  },
  created () {
    this.getHistoryList()
    this.getList()

  },
  mounted () {
    this.userinfo = JSON.parse(sessionStorage.getItem('userinfo') || {}).userId
  },
  methods: {
    preview () {
      this.openPreview = true;
      this.manualPreviewUrl = `${this.previewUrl}${encodeURIComponent(
        `${this.operationManual}项目负责人工作台/业务系统操作说明-项目负责人工作台-管理员授权.xlsx`
      )}&name=${encodeURIComponent("业务系统操作说明-项目负责人工作台-管理员授权.xlsx")}`;
    },
    getList () {
      adminAuthorizeList({ leader: this.$route.params.id }).then(res => {
        this.options = res.data
        res.data.filter(item => item.role === '1').map(item => {
          this.adminList.push(item.userId)
          this.form.push(item.userId)
        })
      })
    },
    getHistoryList () {
      this.loading = true
      this.queryParams.leader = this.$route.params.id;
      adminAuthorizeHistoryList(this.queryParams).then(res => {
        this.total = res.total
        this.tableData = res.rows
        this.loading = false
      })
    },
    changevalue (val) {
      let a = new Set(this.form);
      let b = new Set(val);
      if (this.form.length > val.length) {
        let arr = Array.from(new Set([...a].filter(x => !b.has(x))));
        let list = {
          leader: this.userinfo,
          userId: arr[0],
          guid: this.options.filter(item => item.userId === arr[0])[0].guid
        }
        this.$confirm('确认取消该用户授权管理员?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            return cancelAdminAuthorize(list).then(res => {
              this.form = val
              this.getHistoryList()
            })
          }).catch(() => {
            this.adminList.push(arr[0])
          })
      }
      else {
        let arr = Array.from(new Set([...b].filter(x => !a.has(x))));
        let list = {
          leader: this.userinfo,
          userId: arr[0],
          guid: this.options.filter(item => item.userId === arr[0])[0].guid
        }
        this.$confirm('确认给该用户授权管理员?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            return addAdminAuthorize(list).then(res => {
              if (res.code === 200) {
                this.form = val
                this.getHistoryList()
              }
            })
          }).catch(() => {
            this.adminList = this.adminList.filter(item => item != arr[0])
          })

      }
    },
    resetQuery () {
      this.queryParams.nickName = undefined
      this.queryParams.pageNum = 1
      this.queryParams.remark = undefined
      this.getHistoryList()
    },
    tableSortChangeHistory (column) {
      this.queryParams.pageNum = 1;
      if (column.order === "descending") {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "desc";
      } else {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "asc";
      }
      this.getHistoryList();
    }
  }
}
</script>
<style lang="scss" scoped>
.input-wrap {
  padding: 8px;
  display: flex;

  .input-label {
    min-width: 1em;
    max-width: 6em;
    color: rgba(0, 0, 0, 0.65);
    margin-right: 8px;
    display: flex;
    align-items: center;
    flex: 0 0 90px;
  }

  .input-content {
    -webkit-box-flex: 1;
    flex-grow: 1;
    .el-select {
      width: 30%;
    }
  }
}
.history {
  margin-top: 60px;
  margin-bottom: 20px;
  padding: 20px 8px;
  border-top: 1px solid #dfe6ec;
  color: rgba(0,0,0,0.65);
}
/deep/.el-tag.el-tag--info {
    background-color: #1890ff;
    border-color: #e9e9eb;
    color: #ffffff;
    font-size: 16px;
}
.tipTag {
    margin-left: 20px;
    height: 40px;
    line-height: 40px;
}
</style>